package testJdk.io;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName : com.io.TestSortFile
 * @Description : 类描述
 * Created by cyd on 2020-12-07 23:39:59
 */
public class TestSortFile {
    public static void main(String[] args) {
        //格式化时间字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        File file = new File("C:\\Windows");
        //判断其是否是目录
        if (file.isDirectory()) {
            //得到这个目录下的文件[文件夹]信息
            File[] files = file.listFiles();
            //冒泡排序
            for (int i = 0; i < files.length - 1; i++) {
                for (int j = 0; j < files.length - 1 - i; j++) {
                    if (files[j].length() < files[j + 1].length()) {
                        File temp = files[j + 1];
                        files[j + 1] = files[j];
                        files[j] = temp;
                    }
                }
            }
            //输出结果
            Date date = new Date();
            for (File f : files) {
                long lsatLong = f.lastModified();
                //Date date = new Date(f.lastModified());
                //格式化成一个字符串
                date.setTime(lsatLong);
                String sdfStr = sdf.format(date);
                System.out.print("文件名(目录名):" + f.getName() +
                        ",[长度]" + f.length() +
                        ",是否是文件" + f.isFile() +
                        ",上次修改时间:" + sdfStr +
                        ",是否只读:" + !(f.canWrite()));
                System.out.println();
            }
        }
    }
}
